Note: rosrecord is being replaced by rosbag. The rosbag tutorial may be more appropriate.

(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Recording and playing back data

Description: This tutorial will teach you how to record data from a running ROS system into a so-called bag file, and then to play back the data to produce similar behavior in a running system

Keywords: data, rosrecord, rosplay, bag

Tutorial Level: BEGINNER

Next Tutorial: Producing filtered bag files using rosrebag

Tutorial setup

In order to execute this tutorial you will need to have built the package turtle_teleop and the packages on which it depends. This can be accomplished in your full ROS checkout by the following commands:

roscd turtle_teleop
rosdep install turtle_teleop
rosmake turtle_teleop

This series of commands will take you to the turtle_teleop directory, install the depedencies required by turtle_teleop, and make turtle_teleop and all its unmade dependencies.

Recording data (creating a bag file)

This section of the tutorial will instruct you how to record topic data from a running ROS system. The topic data will be accumulated in a bag file.

First, execute the following two commands:

roscd turtle_teleop
roslaunch launch/turtle_keyboard.launch

This launch will run two nodes - the turtlesim visualizer and a node that allows for the keyboard control of turtlesim using the arrows keys on the keyboard. If you select the terminal window from which you launched turtle_keyboard, you should see something like the following:

Reading from keyboard
---------------------------
Use arrow keys to move the turtle.

Pressing the arrow keys on the keyboard should cause the turtle to move around the screen. Note that to move the turtle you must have the terminal from which you launched turtlesim selected and not the turtlesim window.

Recording all published topics

First lets examine the full list of topics that are currently being published in the running system. To do this, open a new terminal and execute the command:

rostopic list -v

This should yield the following output:

Published topics:
 * /turtle1/color_sensor [turtlesim/Color] 1 publisher
 * /turtle1/command_velocity [turtlesim/Velocity] 1 publisher
 * /rosout [roslib/Log] 2 publishers
 * /rosout_agg [roslib/Log] 1 publisher
 * /turtle1/pose [turtlesim/Pose] 1 publisher

Subscribed topics:
 * /time [unknown type] 3 subscribers
 * /rosout [roslib/Log] 1 subscriber
 * /clock [unknown type] 3 subscribers
 * /turtle1/command_velocity [turtlesim/Velocity] 1 subscriber

The list of published topics are the only message types that could potentially be recorded in the data log file, as only published messages are recorded. The topic /turtle1/command_velocity is the command message published by turtle_telelop that is taken as input by the turtlesim process. The message /turtle1/color_sensor and /turtle1/pose are output messages published by turtlesim.

We now will record the published data. Open a new terminal window. In this window run the following commands:

mkdir ~/bagfiles
cd ~/bagfiles
rosrecord -a

Here we are just making a temporary directory to record data and then running rosrecord with the option -a, indicating that all published topics should be accumulated in a bag file.

Move back to the terminal window with turtle_teleop and move the turtle around for 10 or so seconds.

In the window running rosrecord exit with a Ctrl-C. Now examine the contents of the directory ~/bagfiles. You should see a file with a name that begins with the year, data, and time and the suffix .bag. This is the bag file that contains all topics published by any node in the time that rosrecord was running.

Examining and playing the bag file

Now that we've recorded a bag file using rosrecord we can examine it and play it back using the command rosplay. First we are going to see what's recorded in the bag file. We can do this using rosplay with the -c option - this command checks the contents of the bag file without playing it back. Execute the following command from the bagfiles directory:

rosplay -c <your bagfile>

You should see something like:

bag: 2009-12-04-15-02-56.bag
version: 1.2
start_time: 1259967777871383000
end_time: 1259967797238692999
length: 19367309999
topics:
  - name: /rosout
    count: 2
    datatype: roslib/Log
    md5sum: acffd30cd6b6de30f120938c17c593fb
  - name: /turtle1/color_sensor
    count: 1122
    datatype: turtlesim/Color
    md5sum: 353891e354491c51aabe32df673fb446
  - name: /turtle1/command_velocity
    count: 23
    datatype: turtlesim/Velocity
    md5sum: 9d5c2dcd348ac8f76ce2a4307bd63a13
  - name: /turtle1/pose
    count: 1121
    datatype: turtlesim/Pose
    md5sum: 863b248d5016ca62ea2e895ae5265cf9

This tells us topic names and types as well as the number (count) of each message topic contained in the bag file. We can see that of the topics being advertised that we saw in the rostopic output, four of the five were actually published over our recording interval. As we ran rosrecord with the -a flag it recorded all messages published by all nodes.

The next step in this tutorial is to replay the bag file to reproduce behavior in the running system. First kill any turtlesim nodes that may be still running from the previous section - a Ctrl-C in the terminal where you launched turtle_keyboard.launch should do it. Then in a terminal window execute the following command:

rosrun turtlesim turtlesim_node

This launches the turtlesim node without launching the keyboard teleop node - we will be commanding the turtle with the contents of our bagfile. In a terminal window run the following command in the directory where you took the original bag file:

rosplay <your bagfile>

In this window you should immediately see something like:

Hit space to pause.
[ INFO] 1260210510.566003000: Sleeping 0.200 seconds after advertising /rosout...
[ INFO] 1260210510.766582000: Done sleeping.

[ INFO] 1260210510.872197000: Sleeping 0.200 seconds after advertising /turtle1/pose...
[ INFO] 1260210511.072384000: Done sleeping.

[ INFO] 1260210511.277391000: Sleeping 0.200 seconds after advertising /turtle1/color_sensor...
[ INFO] 1260210511.477525000: Done sleeping.

In its default mode rosplay will wait for a certain period (.2 seconds) after advertising each message before it actually begins publishing the contents of the bag file. Waiting for some duration allows any subscriber of a message to be alerted that the message has been advertised and that messages may follow. If rosplay publishes messages immediately upon advertising, subscribers may not receive the first several published messages. The waiting period can be specified with the -s option to rosplay.

Eventually the topic /turtle1/command_velocity will be published and the turtle should start moving in turtlesim in a pattern similar to the one you executed from the teleop program. The duration between running rosplay and the turtle moving should be approximately equal to the time between the original rosrecord execution and issuing the commands from the keyboard in the beginning part of the tutorial. You can have rosplay not start at the beginning of the bag file but instead start some duration past the beginning using the -t argument. A final option that may be of interest is the -r option, which allows you to change the rate of publishing by a specified factor. If you execute:

rosplay -r 2 <your bagfile>

You should see the turtle execute a slightly different trajectory - this is the trajectory that would have resulted had you issued your keyboard commands twice as fast.

Recording a subset of the data

When running a complicated system, such as the pr2 software suite, there may be hundreds of topics being published, with some topics, like camera image streams, potentially publishing huge amounts of data. In such a system it is often impractical to write log files consisting of all topics to disk in a single bag file. The rosrecord command supports logging only particular topics to a bag file, allowing a user to only record the topics of interest to them.

If any turtlesim nodes are running exit them and relaunch the keyboard teleop launch file:

roscd turtle_teleop
roslaunch launch/turtle_keyboard.launch

In your bagfiles directory, run the following command:

rosrecord -F subset /turtle1/command_velocity /turtle1/pose

The -F argument tells rosrecord to log to a file named subset.bag, and the topic arguments cause rosrecord to only subscribe to these two topics. Move the turtle around for several seconds using the keyboard arrow commands, and then Ctrl-C the rosrecord.

Now check the contents of the bag file (rosplay -c subset.bag). You should see something like this, with only the indicated topics:

bag: subset.bag
version: 1.2
start_time: 3196900000000
end_time: 3215400000000
length: 18500000000
topics:
  - name: /turtle1/command_velocity
    count: 8
    datatype: turtlesim/Velocity
    md5sum: 9d5c2dcd348ac8f76ce2a4307bd63a13
  - name: /turtle1/pose
    count: 1068
    datatype: turtlesim/Pose
    md5sum: 863b248d5016ca62ea2e895ae5265cf9

The limitations of rosrecord/rosplay

In the previous section you may have noted that the turtle's path may not have exactly mapped to the original keyboard input - the rough shape should have been the same, but the turtle may not have exactly tracked the same path. The reason for this is that the path tracked by turtlesim is very sensitive to small changes in timing in the system, and rosrecord/rosplay are limited in their ability to exactly duplicate the behavior of a running system in terms of when messages are recorded and processed by rosrecord, and when messages are produced and processed when using rosplay. For nodes like turtlesim, where minor timing changes in when command messages are processed can subtly alter behavior, the user should not expect perfectly mimicked behavior.

Wiki: rosrecord/Tutorials/Recording and playing back data (last edited 2010-01-14 20:15:08 by JeremyLeibs)